// pages/cashwithdraw/cashwithdraw.js
import {moneylist,moneywithdrawimm} from "../../api/identify"
let currentPage = 1;
let showCount = 15;
let currentPage2 = 1;
let showCount2 = 15;
let currentPage3 = 1;
let showCount3 = 15;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username:'',
    usericon:'',
    detailList:[],
    remainMoney:'',
    userauth:"",
    showpopup:false,
    totalPage:""
  },
  reflash() {
    moneylist({currentPage,showCount}).then((res)=>{
       
      this.setData({
        detailList:res.data.list,
        username: res.data.user.emcee_name,
        usericon:res.data.user.avator,
        remainMoney: res.data.user.remain_may_money,
        userauth:res.data.user.auth_state,
        totalPage:res.data.totalPage
      })
    })
  },
  // 跳转认证页面
  toidentify() {
    wx.navigateTo({
      url: '/pages/identification/identification',
    })
  },
  // 关闭popup
  closepopup() {
    this.setData({
      showpopup: false
    })
  },
  // 立即提现
  withdrawmoneyimm() {
    if(this.data.userauth !=="2") {
      this.setData({
        showpopup:true
      })
      return
    }
    if(this.data.remainMoney < 3) {
      wx.showToast({
        title: '满3元方可提现！',
        icon:'none'
      })
      return
    }
    moneywithdrawimm().then((res)=>{
       
      if(res.data.flag == "1") {
        wx.showToast({
          title: res.data.msg,
          icon:'none'
        })
        return
      }else if(res.data.flag == "2") {
        let that = this;
        wx.showToast({
          title: res.data.msg,
          icon:'none',
          success: function() {
            if(res) {
              that.onLoad()
            }
          }
        })
        // reflash()
        // this.onLoad()
        return
      }else if(res.data.flag == "3") {
        let that = this;
        wx.showToast({
          title: res.data.msg,
          icon:'none',
          success: function() {
            //  
            if(res) {
              that.onLoad()
            }
          }
        })
        return
      }else if(res.data.flag == "4") {
        let that = this;
        wx.showToast({
          title: res.data.msg,
          icon:'none',
          success:function() {
            if(res) {
              that.onLoad()
            }
          }
        })
        return
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 获取现金明细和用户信息
    currentPage = 1
    moneylist({currentPage,showCount}).then((res)=>{
       
      this.setData({
        detailList:res.data.list,
        username: res.data.user.emcee_name,
        usericon:res.data.user.avator,
        remainMoney: res.data.user.remain_may_money,
        userauth:res.data.user.auth_state,
        totalPage:res.data.totalPage
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    currentPage = 1
    moneylist({currentPage,showCount}).then((res)=>{
       
      this.setData({
        detailList:res.data.list,
        username: res.data.user.emcee_name,
        usericon:res.data.user.avator,
        remainMoney: res.data.user.remain_may_money,
        userauth:res.data.user.auth_state,
        totalPage:res.data.totalPage
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    currentPage++
    if(currentPage <= this.data.totalPage) {
      moneylist({currentPage,showCount}).then((res)=>{
         
        let list = this.data.detailList
        let temparr = list.concat(res.data.list)

        this.setData({
          detailList: temparr
        })
      })
    }else{
      wx.showToast({
        title: '我是有底线的',
        icon:'none'
      })
      currentPage = this.data.totalPage
    }
 
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})